﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Controls;
using Petroleum.UC.Public;
using Petroleum.Model;
using Petroleum.DB.Model;
using Petroleum.Properties;
using Petroleum.Tool;
using Newtonsoft.Json;
using System.Data.SQLite;
using HZH_Controls.Forms;
using System.IO;

namespace Petroleum.UC
{
    public partial class Step5 : UserControl
    {
        FormMain main = new FormMain();
        bool hassave = false;
        List<PressureImg> pressureImgs = new List<PressureImg>();
        public Step5(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            imgcasing.Initialization("Pressure Profile (g/cm3)");
            imgPres.Initialization("Casing Program");
            this.dgv.AutoGenerateColumns = false;
            InitBindData();
        }
        void InitBindData()
        {
            pressureImgs.Clear();
            if (Session.FormationPressure.list != null)
            {
                foreach (var item in Session.FormationPressure.list)
                {
                    if (File.Exists(item.ImgColumnarSection))
                    {
                        PressureImg img = new PressureImg()
                        {
                            ColumnarImg = Image.FromFile(item.ImgColumnarSection),
                            GeologicalDivisions = item.GeologicalDivisions
                        };
                        pressureImgs.Add(img);
                    }
                   
                }
            }
            this.dgv.DataSource = pressureImgs;
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            main.AddControl(new StepADrillingTeamSelect(main));
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step4WellBoreQuality(main));
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //在检测到按Ctrl+V键后，系统无法复制多单元格数据，重写ProcessCmdKey方法，屏蔽系统粘贴事件，使用自定义粘贴事件，在事件中对剪贴板的HTML格式进行处理，获取表数据，更新DataGrid控件内容
            if (keyData == (Keys.V | Keys.Control) && this.dgv.SelectedCells.Count > 0 && !this.dgv.SelectedCells[0].IsInEditMode)
            {
                IDataObject iData = Clipboard.GetDataObject();
                var cell = dgv.Rows[dgv.CurrentRow.Index].Cells[dgv.CurrentCell.ColumnIndex];
                if (cell != null)
                    if (iData.GetDataPresent(DataFormats.FileDrop))
                        cell.Value = Image.FromFile(Array.ConvertAll<object, string>((object[])iData.GetData(DataFormats.FileDrop), Convert.ToString)[0]);
                if (iData.GetDataPresent(DataFormats.Bitmap))
                    cell.Value = (Bitmap)iData.GetData(DataFormats.Bitmap, true);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        void SaveData()
        {
            //Session.FormationPressure.list = (List<Pressure>)this.dgv.DataSource;
            Session.FormationPressure.ImgCasingProgram = imgcasing.ImgUrl;
            Session.FormationPressure.ImgPressureProfile = imgPres.ImgUrl;
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var casing = main.projects.Where(n => n.TableName == TableNames.FormationPressure.ToString()).FirstOrDefault();
            if (casing != null)
            {
                Session.FormationPressure = JsonConvert.DeserializeObject<FormationPressure>(casing.TableData);
                InitBindData();
                imgcasing.ImgUrl = Session.FormationPressure.ImgCasingProgram;
                imgPres.ImgUrl = Session.FormationPressure.ImgPressureProfile;
                if (!string.IsNullOrEmpty(imgcasing.ImgUrl)&& File.Exists(imgcasing.ImgUrl))
                    imgcasing.picImg.Image = Image.FromFile(imgcasing.ImgUrl);
                if (!string.IsNullOrEmpty(imgPres.ImgUrl)&& File.Exists(imgcasing.ImgUrl))
                    imgPres.picImg.Image = Image.FromFile(imgPres.ImgUrl);
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            if (!hassave)
            {
                SaveData();
            }
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.FormationPressure.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.FormationPressure.ToString(), JsonConvert.SerializeObject(Session.FormationPressure));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.FormationPressure.ToString(), JsonConvert.SerializeObject(Session.FormationPressure), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex < dgv.Rows.Count - 1)
            {
                if (e.ColumnIndex == 0)
                {
                    var imageColumn = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    if (imageColumn != null)
                    {
                        var imageColumnstr = dgv.Rows[e.RowIndex].Cells[2];
                        imageColumn.Value = Image.FromFile(imageColumnstr.Value.ToString());
                    }
                }
            }
        }
    }
}
